<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('kefu.user.identity')"
                        type="radio"
                        v-model="baTable.form.items!.identity"
                        prop="identity"
                        :data="{
                            content: {
                                tourist: t('kefu.user.identity tourist'),
                                user: t('kefu.user.identity user'),
                                csr: t('kefu.user.identity csr'),
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('kefu.user.identity') })"
                    />
                    <FormItem
                        :label="t('kefu.user.group_ids')"
                        type="remoteSelects"
                        v-model="baTable.form.items!.group_ids"
                        prop="group_ids"
                        :input-attr="{ pk: 'group.id', field: 'name', remoteUrl: '/admin/kefu.Group/index' }"
                        :placeholder="t('Please select field', { field: t('kefu.user.group_ids') })"
                    />
                    <FormItem
                        :label="t('kefu.user.user_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.user_id"
                        prop="user_id"
                        :input-attr="{
                            pk: 'user.id',
                            field: 'nickname',
                            remoteUrl: '/admin/user.User/index',
                            emptyValues: ['', null, undefined, 0],
                            valueOnClear: 0,
                        }"
                        :placeholder="t('Please select field', { field: t('kefu.user.user_id') })"
                    />
                    <FormItem
                        :label="t('kefu.user.admin_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.admin_id"
                        prop="admin_id"
                        :input-attr="{
                            pk: 'admin.id',
                            field: 'nickname',
                            remoteUrl: '/admin/auth.Admin/index',
                            emptyValues: ['', null, undefined, 0],
                            valueOnClear: 0,
                        }"
                        :placeholder="t('Please select field', { field: t('kefu.user.admin_id') })"
                    />
                    <FormItem
                        :label="t('kefu.user.nickname')"
                        type="string"
                        v-model="baTable.form.items!.nickname"
                        prop="nickname"
                        :placeholder="t('Please input field', { field: t('kefu.user.nickname') })"
                    />
                    <FormItem
                        :label="t('kefu.user.contacts')"
                        type="textarea"
                        v-model="baTable.form.items!.contacts"
                        prop="contacts"
                        :input-attr="{ rows: 3 }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('kefu.user.contacts') })"
                    />
                    <FormItem
                        :label="t('kefu.user.referrer')"
                        type="string"
                        v-model="baTable.form.items!.referrer"
                        prop="referrer"
                        :placeholder="t('Please input field', { field: t('kefu.user.referrer') })"
                    />
                    <FormItem
                        :label="t('kefu.user.gender')"
                        type="radio"
                        v-model="baTable.form.items!.gender"
                        prop="gender"
                        :data="{ content: { '0': t('kefu.user.gender 0'), '1': t('kefu.user.gender 1'), '2': t('kefu.user.gender 2') } }"
                        :placeholder="t('Please select field', { field: t('kefu.user.gender') })"
                    />
                    <FormItem
                        :label="t('kefu.user.company')"
                        type="string"
                        v-model="baTable.form.items!.company"
                        prop="company"
                        :placeholder="t('Please input field', { field: t('kefu.user.company') })"
                    />
                    <FormItem
                        :label="t('kefu.user.occupation')"
                        type="string"
                        v-model="baTable.form.items!.occupation"
                        prop="occupation"
                        :placeholder="t('Please input field', { field: t('kefu.user.occupation') })"
                    />
                    <FormItem
                        :label="t('kefu.user.last_login_time')"
                        type="datetime"
                        v-model="baTable.form.items!.last_login_time"
                        prop="last_login_time"
                        :placeholder="t('Please select field', { field: t('kefu.user.last_login_time') })"
                    />
                    <FormItem
                        :label="t('kefu.user.last_login_ip')"
                        type="string"
                        v-model="baTable.form.items!.last_login_ip"
                        prop="last_login_ip"
                        :placeholder="t('Please input field', { field: t('kefu.user.last_login_ip') })"
                    />
                    <FormItem
                        :label="t('kefu.user.join_time')"
                        type="datetime"
                        v-model="baTable.form.items!.join_time"
                        prop="join_time"
                        :placeholder="t('Please select field', { field: t('kefu.user.join_time') })"
                    />
                    <FormItem
                        :label="t('kefu.user.join_ip')"
                        type="string"
                        v-model="baTable.form.items!.join_ip"
                        prop="join_ip"
                        :placeholder="t('Please input field', { field: t('kefu.user.join_ip') })"
                    />
                    <FormItem
                        v-if="baTable.form.items!.identity == 'csr'"
                        :label="t('kefu.user.csr_welcome_msg')"
                        type="textarea"
                        v-model="baTable.form.items!.csr_welcome_msg"
                        prop="csr_welcome_msg"
                        :input-attr="{
                            rows: 3,
                        }"
                        :attr="{
                            blockHelp: '被作为客服代表自动分配给用户时发送的欢迎消息',
                        }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('kefu.user.csr_welcome_msg') })"
                    />
                    <FormItem
                        v-if="baTable.form.items!.identity == 'csr'"
                        :label="t('kefu.user.csr_maximum_reception')"
                        type="number"
                        prop="csr_maximum_reception"
                        :input-attr="{ step: 1, valueOnClear: 0 }"
                        v-model.number="baTable.form.items!.csr_maximum_reception"
                        :placeholder="t('Please input field', { field: t('kefu.user.csr_maximum_reception') })"
                    />
                    <FormItem
                        v-if="baTable.form.items!.identity == 'csr'"
                        :label="t('kefu.user.csr_reception_count')"
                        type="number"
                        prop="csr_reception_count"
                        :input-attr="{ step: 1, valueOnClear: 0 }"
                        v-model.number="baTable.form.items!.csr_reception_count"
                        :placeholder="t('Please input field', { field: t('kefu.user.csr_reception_count') })"
                    />
                    <FormItem
                        v-if="baTable.form.items!.identity == 'csr'"
                        :label="t('kefu.user.csr_last_reception_time')"
                        type="datetime"
                        v-model="baTable.form.items!.csr_last_reception_time"
                        prop="csr_last_reception_time"
                        :placeholder="t('Please select field', { field: t('kefu.user.csr_last_reception_time') })"
                    />
                    <FormItem
                        :label="t('kefu.user.remark')"
                        type="textarea"
                        v-model="baTable.form.items!.remark"
                        prop="remark"
                        :input-attr="{ rows: 3 }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('kefu.user.remark') })"
                    />
                    <FormItem
                        :label="t('kefu.user.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{
                            content: {
                                offline: t('kefu.user.status offline'),
                                online: t('kefu.user.status online'),
                                busy: t('kefu.user.status busy'),
                                invisible: t('kefu.user.status invisible'),
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('kefu.user.status') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    last_login_time: [buildValidatorData({ name: 'date', title: t('kefu.user.last_login_time') })],
    join_time: [buildValidatorData({ name: 'date', title: t('kefu.user.join_time') })],
    csr_maximum_reception: [buildValidatorData({ name: 'number', title: t('kefu.user.csr_maximum_reception') })],
    csr_reception_count: [buildValidatorData({ name: 'number', title: t('kefu.user.csr_reception_count') })],
    csr_last_reception_time: [buildValidatorData({ name: 'date', title: t('kefu.user.csr_last_reception_time') })],
    create_time: [buildValidatorData({ name: 'date', title: t('kefu.user.create_time') })],
})
</script>

<style scoped lang="scss"></style>
